﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;

namespace EasyVideoEditor
{
    /// <summary>
    /// Interaction logic for CroppingControl.xaml
    /// </summary>
    public partial class CroppingControl : UserControl
    {
        public CroppingControl()
        {
            InitializeComponent();
        }

        private void thumb1_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
        {

        }

        private void thumb1_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
        {
            //Canvas.SetLeft(thumb1, Canvas.GetLeft(thumb1) + e.HorizontalChange);
            //Canvas.SetTop(thumb1, Canvas.GetTop(thumb1) + e.VerticalChange);
        }

        private void thumb1_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
        {
            string part = ((Thumb)sender).Tag.ToString();

            double newLeft = Canvas.GetLeft((Thumb)sender) + e.HorizontalChange;
            double newTop = Canvas.GetTop((Thumb)sender) + e.VerticalChange;

            double halfWidth = ((Thumb)sender).Width / 2.0;
            double halfHeight = ((Thumb)sender).Height / 2.0;

            if (newLeft < 0.0 - halfWidth)
            {
                newLeft = 0 - halfWidth + 0.01;
            }
            else if (newLeft > canvasHolder.ActualWidth - halfWidth)
            {
                newLeft = canvasHolder.ActualWidth - halfWidth - 0.01;
            }
            if (newTop < 0.0 - halfHeight)
            {
                newTop = 0 - halfHeight + 0.01;
            }
            else if (newTop > canvasHolder.ActualHeight - halfHeight)
            {
                newTop = canvasHolder.ActualHeight - halfHeight - 0.01;
            }

            switch (part)
            {
                case "NW":
                    if (newLeft > 0.0 - halfWidth && newLeft < canvasHolder.ActualWidth + halfWidth
                        && newTop > 0.0 - halfHeight && newTop < canvasHolder.ActualHeight + halfHeight
                        && newLeft < Canvas.GetLeft(thumbNE)
                        && newTop < Canvas.GetTop(thumbSW))
                    {
                        Canvas.SetLeft((Thumb)sender, newLeft);
                        Canvas.SetTop((Thumb)sender, newTop);
                        Canvas.SetLeft(thumbSW, newLeft);
                        Canvas.SetTop(thumbNE, newTop);
                    }
                    break;
                case "SW":
                    if (newLeft > 0.0 - halfWidth && newLeft < canvasHolder.ActualWidth + halfWidth
                        && newTop > 0.0 - halfHeight && newTop < canvasHolder.ActualHeight - halfHeight
                        && newLeft < Canvas.GetLeft(thumbSE)
                        && newTop > Canvas.GetTop(thumbNW))
                    {
                        Canvas.SetLeft((Thumb)sender, newLeft);
                        Canvas.SetTop((Thumb)sender, newTop);
                        Canvas.SetLeft(thumbNW, newLeft);
                        Canvas.SetTop(thumbSE, newTop);
                    }
                    break;
                case "NE":
                    if (newLeft > 0.0 - halfWidth && newLeft < canvasHolder.ActualWidth + halfWidth
                        && newTop > 0.0 - halfHeight && newTop < canvasHolder.ActualHeight + halfHeight
                        && newLeft > Canvas.GetLeft(thumbNW)
                        && newTop < Canvas.GetTop(thumbSE))
                    {
                        Canvas.SetLeft((Thumb)sender, newLeft);
                        Canvas.SetTop((Thumb)sender, newTop);
                        Canvas.SetLeft(thumbSE, newLeft);
                        Canvas.SetTop(thumbNW, newTop);
                    }
                    break;
                case "SE":
                    if (newLeft > 0.0 - halfWidth && newLeft < canvasHolder.ActualWidth + halfWidth
                        && newTop > 0.0 - halfHeight && newTop < canvasHolder.ActualHeight + halfHeight
                        && newLeft > Canvas.GetLeft(thumbSW)
                        && newTop > Canvas.GetTop(thumbNE))
                    {
                        Canvas.SetLeft((Thumb)sender, newLeft);
                        Canvas.SetTop((Thumb)sender, newTop);
                        Canvas.SetLeft(thumbNE, newLeft);
                        Canvas.SetTop(thumbSW, newTop);
                    }
                    break;
            }

            rectangleCropped.Width = Canvas.GetLeft(thumbNE) - Canvas.GetLeft(thumbNW);
            rectangleCropped.Height = Canvas.GetTop(thumbSE) - Canvas.GetTop(thumbNE);
            Canvas.SetLeft(rectangleCropped, Canvas.GetLeft(thumbNW) + halfWidth);
            Canvas.SetTop(rectangleCropped, Canvas.GetTop(thumbNW) + halfHeight);

            if (CroppingChanged != null)
            {
                //Thickness newCrop = new Thickness();
                //newCrop.Left = ((thumbNW.ActualWidth / 2.0) / (canvasHolder.Width / (double)Source.PixelWidth)) + ((Canvas.GetLeft(thumbNW)) / (canvasHolder.Width / (double)Source.PixelWidth));
                //newCrop.Right = (canvasHolder.Width / (canvasHolder.Width / (double)Source.PixelWidth)) - (Canvas.GetLeft(thumbNE) / (canvasHolder.Width / (double)Source.PixelWidth)) - ((thumbSW.ActualWidth / 2.0) / (canvasHolder.Width / (double)Source.PixelWidth));
                //newCrop.Top = (Canvas.GetTop(thumbNW) / (canvasHolder.Height / (double)Source.PixelHeight)) - ((thumbNW.ActualHeight / 2.0) / (canvasHolder.Height / (double)Source.PixelHeight));
                //CroppingChanged(sender, newCrop);
            }
        }

        private BitmapSource _Source;
        public BitmapSource Source
        {
            get
            {
                return _Source;
            }
            set
            {
                _Source = value;
                canvasHolder.Background = new ImageBrush(_Source);
                if (_Source.PixelHeight < _Source.PixelWidth)
                {
                    canvasHolder.Width = this.Width;
                    canvasHolder.Height = ((this.Width / _Source.PixelWidth) * _Source.PixelHeight);
                }
                else
                {
                    canvasHolder.Height = this.Height;
                    canvasHolder.Width = ((this.Height / _Source.PixelHeight) * _Source.PixelWidth);
                }
                Canvas.SetLeft(canvasHolder, (this.Width / 2) - (canvasHolder.Width / 2));
                Canvas.SetTop(canvasHolder, (this.Height / 2) - (canvasHolder.Height / 2));
            }
        }

        private Thickness _CropAmount;
        public Thickness CropAmount
        {
            get
            {
                return _CropAmount;
            }
            set
            {
                _CropAmount = value;
                if (Source != null)
                {
                    try
                    {
                        Canvas.SetLeft(thumbNW, ((canvasHolder.Width / (double)Source.PixelWidth) * value.Left) - (thumbNW.ActualWidth / 2.0));
                        Canvas.SetLeft(thumbSW, ((canvasHolder.Width / (double)Source.PixelWidth) * value.Left) - (thumbSW.ActualWidth / 2.0));
                        Canvas.SetLeft(thumbNE, canvasHolder.Width - ((canvasHolder.Width / (double)Source.PixelWidth) * value.Right) - (thumbSW.ActualWidth / 2.0));
                        Canvas.SetLeft(thumbSE, canvasHolder.Width - ((canvasHolder.Width / (double)Source.PixelWidth) * value.Right) - (thumbSW.ActualWidth / 2.0));

                        Canvas.SetTop(thumbNW, ((canvasHolder.Height / (double)Source.PixelHeight) * value.Top) - (thumbNW.ActualHeight / 2.0));
                        Canvas.SetTop(thumbSW, canvasHolder.Height - ((canvasHolder.Height / (double)Source.PixelHeight) * value.Bottom) - (thumbSW.ActualHeight / 2.0));
                        Canvas.SetTop(thumbNE, ((canvasHolder.Height / (double)Source.PixelHeight) * value.Top) - (thumbSW.ActualHeight / 2.0));
                        Canvas.SetTop(thumbSE, canvasHolder.Height - ((canvasHolder.Height / (double)Source.PixelHeight) * value.Bottom) - (thumbSW.ActualHeight / 2.0));

                        rectangleCropped.Width = Canvas.GetLeft(thumbNE) - Canvas.GetLeft(thumbNW);
                        rectangleCropped.Height = Canvas.GetTop(thumbSE) - Canvas.GetTop(thumbNE);
                        Canvas.SetLeft(rectangleCropped, Canvas.GetLeft(thumbNW) + (thumbSW.ActualWidth / 2.0));
                        Canvas.SetTop(rectangleCropped, Canvas.GetTop(thumbNW) + (thumbSW.ActualHeight / 2.0));
                    }
                    catch 
                    { 
                    }
                }
            }
        }

        #region Events
        public event CroppingChangedEventHandler CroppingChanged;
        public delegate void CroppingChangedEventHandler(object sender, Thickness NewCropValues);
        #endregion
    }
}
